﻿// -----------------------------------------------------------------------
// <copyright file="DmHangSanXuatDataAccess.cs" company="">
// TODO: Copyright by dangppsoft@gmail.com.
// </copyright>
// -----------------------------------------------------------------------
namespace ManagerClinic.Core.DataAccess
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using ManagerClinic.Core.Common;
    using System.Data.Common;
    using System.Data;
    using Microsoft.Practices.EnterpriseLibrary.Data;
    using Microsoft.Practices.EnterpriseLibrary.Common;
    using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
    using ManagerClinic.Ultilities.Ultility;

    public class DmHangSanXuatDataAccess
    {
        public bool InsertDmHangSanXuat(DmHangSanXuatEntity obj)
        {
            try
            {
                string sql = "spDmHangSanXuat_Insert";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaHang", DbType.String, obj.MaHang);
                db.AddInParameter(cmd, "TenHang", DbType.String, obj.TenHang);
                db.AddInParameter(cmd, "DiaChi", DbType.String, obj.DiaChi);
                db.AddInParameter(cmd, "DienThoai", DbType.String, obj.DienThoai);
                db.AddInParameter(cmd, "Fax", DbType.String, obj.Fax);
                db.AddInParameter(cmd, "Email", DbType.String, obj.Email);
                db.AddInParameter(cmd, "Website", DbType.String, obj.Website);
                db.AddInParameter(cmd, "MoTa", DbType.String, obj.MoTa);
                db.AddInParameter(cmd, "MaQuocGia", DbType.String, obj.MaQuocGia);
                db.AddInParameter(cmd, "TrangThaiSuDung", DbType.Boolean, obj.TrangThaiSuDung);
                int result = (int)db.ExecuteNonQuery(cmd);
                return (result > 0) ? true : false;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("InsertDmHangSanXuat", ex.Message); throw ex; }
        }

        public bool EditDmHangSanXuat(DmHangSanXuatEntity obj)
        {
            try
            {
                string sql = "spDmHangSanXuat_Update";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaHang", DbType.String, obj.MaHang);
                db.AddInParameter(cmd, "TenHang", DbType.String, obj.TenHang);
                db.AddInParameter(cmd, "DiaChi", DbType.String, obj.DiaChi);
                db.AddInParameter(cmd, "DienThoai", DbType.String, obj.DienThoai);
                db.AddInParameter(cmd, "Fax", DbType.String, obj.Fax);
                db.AddInParameter(cmd, "Email", DbType.String, obj.Email);
                db.AddInParameter(cmd, "Website", DbType.String, obj.Website);
                db.AddInParameter(cmd, "MoTa", DbType.String, obj.MoTa);
                db.AddInParameter(cmd, "MaQuocGia", DbType.String, obj.MaQuocGia);
                db.AddInParameter(cmd, "TrangThaiSuDung", DbType.Boolean, obj.TrangThaiSuDung);
                int result = (int)db.ExecuteNonQuery(cmd);
                return (result > 0) ? true : false;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("UpdateDmHangSanXuat", ex.Message); throw ex; }
        }

        public bool DeleteDmHangSanXuat(string maHang)
        {
            try
            {
                string sql = "spDmHangSanXuat_DeleteByID";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaHang", DbType.String, maHang);
                int result = (int)db.ExecuteNonQuery(cmd);
                return (result > 0) ? true : false;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("DeleteDmHangSanXuat", ex.Message); throw ex; }
        }

        public DmHangSanXuatEntity GetDmHangSanXuat(string maHang)
        {
            try
            {
                DmHangSanXuatEntity obj = null;
                string sql = "spDmHangSanXuat_SelectByID";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaHang", DbType.String, maHang);
                using (IDataReader reader = db.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        obj = new DmHangSanXuatEntity();
                        obj.MaHang = reader["MaHang"].ToString();
                        obj.TenHang = reader["TenHang"].ToString();
                        obj.DiaChi = reader["DiaChi"].ToString();
                        obj.DienThoai = reader["DienThoai"].ToString();
                        obj.Fax = reader["Fax"].ToString();
                        obj.Email = reader["Email"].ToString();
                        obj.Website = reader["Website"].ToString();
                        obj.MoTa = reader["MoTa"].ToString();
                        obj.MaQuocGia = reader["MaQuocGia"].ToString();
                        obj.TrangThaiSuDung = String.IsNullOrEmpty(reader["TrangThaiSuDung"].ToString()) == true ? false : (bool)reader["TrangThaiSuDung"];
                    }
                }
                return obj;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("GetDmHangSanXuat", ex.Message); throw ex; }
        }

        public List<DmHangSanXuatEntity> GetDmHangSanXuatList()
        {
            try
            {
                List<DmHangSanXuatEntity> list = new List<DmHangSanXuatEntity>();
                DmHangSanXuatEntity obj = null;
                string sql = "spDmHangSanXuat_SelectAll";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                using (IDataReader reader = db.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        obj = new DmHangSanXuatEntity();
                        obj.MaHang = reader["MaHang"].ToString();
                        obj.TenHang = reader["TenHang"].ToString();
                        obj.DiaChi = reader["DiaChi"].ToString();
                        obj.DienThoai = reader["DienThoai"].ToString();
                        obj.Fax = reader["Fax"].ToString();
                        obj.Email = reader["Email"].ToString();
                        obj.Website = reader["Website"].ToString();
                        obj.MoTa = reader["MoTa"].ToString();
                        obj.MaQuocGia = reader["MaQuocGia"].ToString();
                        obj.TrangThaiSuDung = String.IsNullOrEmpty(reader["TrangThaiSuDung"].ToString()) == true ? false : (bool)reader["TrangThaiSuDung"];
                        list.Add(obj);
                    }
                }
                return list;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("GetDmHangSanXuatList", ex.Message); throw ex; }
        }

    }
}